package com.umeox.watch.moto.launcher.ui;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.LogUtils;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.SecondaryMenuListAdapter;
import com.umeox.watch.moto.launcher.bean.AppInfo;
import com.umeox.watch.moto.launcher.constant.Constant;
import com.umeox.watch.moto.launcher.fragment.AppListConfigureInfo;

import java.util.ArrayList;


public class SecondaryMenuActivity extends ThemeAppCompatActivity implements SecondaryMenuListAdapter.OnClickListener {

    private static final String EXTRA_GROUP_INDEX = "group_index";

    public RecyclerView mAppListView;

    private int selectedAppGroupIndex = -1;

    private BindStatusReceiver mBindStatusReceiver;

    private ArrayList<AppInfo> appInfoList = new ArrayList<>();

    public static void launch(Context context, int groupIndex) {
        Intent intent = new Intent(context, SecondaryMenuActivity.class);
        intent.putExtra(EXTRA_GROUP_INDEX, groupIndex);
        context.startActivity(intent);
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary_menu);
        registerBindReceiver();
        selectedAppGroupIndex = getIntent().getIntExtra(EXTRA_GROUP_INDEX, -1);
        LogUtils.i("SecondaryMenuActivity", "selectedAppGroupIndex = " + selectedAppGroupIndex);
        if (selectedAppGroupIndex == -1) {
            finish();
            return;
        }
        mAppListView = findViewById(R.id.app_list_view);
        initData(DataProvider.isActivated());
    }

    //处于二级菜单界面时 如果绑定状态发生变化，退出二级菜单页面
    private void registerBindReceiver() {
        mBindStatusReceiver = new BindStatusReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.IntentConstant.INTENT_ACTIVE);
        intentFilter.addAction(Constant.IntentConstant.INTENT_UNBOUND);
        registerReceiver(mBindStatusReceiver, intentFilter);
    }

    public void initData(boolean isBind) {
        appInfoList = AppListConfigureInfo.INSTANCE.getAppGroupList(isBind).get(selectedAppGroupIndex).appInfos;
        SecondaryMenuListAdapter adapter = new SecondaryMenuListAdapter(this, appInfoList);
        mAppListView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

                int position = parent.getChildAdapterPosition(view);
                if (position == 0) {
                    outRect.top += dp2px(22f);
                }

                if (position == adapter.getItemCount() - 1) {
                    outRect.bottom += dp2px(20f);
                }
            }
        });
        mAppListView.setAdapter(adapter);
        adapter.setOnClickListener(this);
        mAppListView.setHasFixedSize(true);
    }

    @Override
    public void onPositionClick(int position) {
        if (getLifecycle().getCurrentState() != Lifecycle.State.RESUMED) {
            return;
        }
        AppInfo app = appInfoList.get(position);
        AppListConfigureInfo.handleAppAction(app, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBindStatusReceiver != null) {
            unregisterReceiver(mBindStatusReceiver);
            mBindStatusReceiver = null;
        }
    }

    public int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private class BindStatusReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }

            if (action.equals(Constant.IntentConstant.INTENT_ACTIVE)
            || action.equals(Constant.IntentConstant.INTENT_UNBOUND)) {
               finish();
            }
        }
    }

}
